package com.totem.customer.controller;

import cn.hutool.core.lang.Assert;
import com.totem.base.CacheHolder;
import com.totem.base.constants.CacheConstants;
import com.totem.base.constants.ErrorCode;
import com.totem.base.exception.BaseException;
import com.totem.base.model.PageVO;
import com.totem.base.model.Result;
import com.totem.base.mybatis.UserIdContext;
import com.totem.base.mybatis.controller.BaseController;
import com.totem.customer.api.ITCustomerServiceApi;
import com.totem.customer.model.TCustomers;
import com.totem.customer.serivce.ITCustomerService;
import com.totem.customer.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class CustomerController extends BaseController<ITCustomerService, TCustomers> implements ITCustomerServiceApi {
    @Autowired
    private CacheHolder cacheHolder;
    @Override
    public Result<Boolean> create(CustomerCreateVO vo) {
        TCustomers tCustomers = TCustomers.builder().build();
        BeanUtils.copyProperties(vo, tCustomers);
        tCustomers.setPassword(service.defaultPassword());

        boolean result = service.createCustomer(tCustomers);
        return Result.result("创建客户信息成功", "创建客户信息失败", result);
    }

    @Override
    public Result<Boolean> update(CustomerUpdateVO vo) {
        Assert.notNull(vo.getId(), "客户Id必填");
        TCustomers tCustomers = TCustomers.builder().build();
        BeanUtils.copyProperties(vo, tCustomers);

        return Result.result("修改客户信息成功", "修改客户信息失败", service.updateById(tCustomers));
    }

    @Override
    public Result<Boolean> delete(CustomerDelVO vo) {
        Assert.notNull(vo.getId(), "客户Id必填");
        boolean result = service.removeById(vo.getId());
        return Result.result("删除客户成功", "删除客户失败", result);
    }

    @Override
    public Result<PageVO<CustomerVO>> page(CustomerPageVO vo) {
        return Result.success(service.page(vo));
    }

    @Override
    public Result<CustomerVO> getById(Long id) {
        TCustomers tCustomers = service.getById(id);
        CustomerVO vo = new CustomerVO();
        BeanUtils.copyProperties(tCustomers, vo);
        return Result.success(vo);
    }

    @Override
    public Result<CustomerLoginVO> login(CustomerLoginVO vo) {
        log.info("user {} login by {}", vo.getNickname(), vo.getPassword());

        Long userId = UserIdContext.getUserId();
        if(userId==null){
            throw new BaseException(ErrorCode.UNAUTHORIZED);
        }
        TCustomers sysUser = service.getById(userId);
        if(sysUser==null){
            throw new BaseException(ErrorCode.NOT_FOUND);
        }
        BeanUtils.copyProperties(sysUser, vo);
        vo.setPassword(null);

        Object tokenObj = cacheHolder.get(CacheConstants.CACHE_TOKEN +userId);
        if(tokenObj==null){
            throw new BaseException(ErrorCode.UNAUTHORIZED);
        }
        String token = tokenObj.toString();
        vo.setToken(token);
        return Result.success(vo);
    }

    @Override
    public Result<Boolean> logout() {
        // 清理业务数据
        return Result.success(Boolean.TRUE);
    }
}
